package ${packageName};

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ${className}AdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ${className}Repository ${objectName}Repository;

    @RequestMapping("/${objectName}/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = ${objectName}Repository.selectCount(queryParam);
        List<${className}> list = ${objectName}Repository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "${objectName}/list";
    }

    @RequestMapping("/${objectName}/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        ${className} ${objectName} = new ${className}();
        if(id!=null){
            ${objectName} = ${objectName}Repository.selectByPrimaryKey(id);
        }
        model.addAttribute("${objectName}",${objectName});
        return "${objectName}/operate";
    }

    @PostMapping("/${objectName}/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(${className} ${objectName}, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(${objectName}.getId()!=null){
                ${objectName}Repository.updateByPrimaryKey(${objectName});
            }else {
                ${objectName}.init();
                Integer a = ${objectName}Repository.insert(${objectName});
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/${objectName}/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            ${className} ${objectName} = new ${className}();
            ${objectName}.setId(id);
            ${objectName}.setValid(Boolean.FALSE);
            ${objectName}Repository.updateByPrimaryKeySelective(${objectName});
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
